//
//  RecommendViewController.swift
//  DouYuZB
//
//  Created by DSDL on 2016/10/30.
//  Copyright © 2016年 鼎商动力. All rights reserved.
//

import UIKit
import Alamofire
//MARK: - 设置常量
private let kCycleViewH: CGFloat = kScreenW * 3 / 8
private let kGameViewH: CGFloat = 90


class RecommendViewController: BaseAnchorViewController {
    
    //MARK: - 懒加载属性
    fileprivate lazy var recommendVM: RecommendViewModel = RecommendViewModel()
    //轮播图
    fileprivate lazy var cycleView: RecommendCycleView = {
        let cycle  = RecommendCycleView.fromNib(nibName: "RecommendCycleView")
        cycle.frame = CGRect(x: 0, y: -(kCycleViewH + kGameViewH), width: kScreenW, height: kCycleViewH)
        return cycle as! RecommendCycleView
    }()
    fileprivate lazy var gameView: RecommendGameView = {
        let gameView = RecommendGameView.fromNib(nibName: "RecommendGameView")
        gameView.frame = CGRect(x: 0, y: -kGameViewH, width: kScreenW, height: kGameViewH)
        return gameView as! RecommendGameView
    }()
    
}

//MARK: - 设置UI界面内容
extension RecommendViewController {
    override func setupUI(){
        //1.先调用 super UI
        super.setupUI()
        //2.将Cycle 添加到控制器的UICollectionView中
        collectionView.addSubview(cycleView)
        //3. 添加game到UICollectionView中
        collectionView.addSubview(gameView)
        //4.设置collectionView的内边距
        collectionView.contentInset = UIEdgeInsetsMake(kCycleViewH+kGameViewH, 0, 0, 0)
    }
}

//MARK: -  请求数据
extension RecommendViewController {
    
    override func loadData(){
        //0.给 父类ViewModel
        baseVM = recommendVM
        //1.请求推荐数据
        recommendVM.requestData {
            //1.展示推荐数据
            self.collectionView.reloadData()
            //2.将数据传递给GameView
            var groups = self.recommendVM.anchorGroup
            //2.1 移除前两组数据
            groups.removeFirst()
            groups.removeFirst()
            //2.2添加更多组
            let moreGroup = AnchorGroup()
            moreGroup.tag_name = "更多"
            groups.append(moreGroup)
            
            self.gameView.groupsArr = groups
            
            //3.数据请求完成
            self.loadDataFinish()
        }
        
        //2.请求轮播数据
        recommendVM.requestCycleData { [weak self] in
            if let weakSelf = self{
                //1.展示推荐数据
                weakSelf.cycleView.cycleModel = weakSelf.recommendVM.cycleModels
            }
        }
        
    }
    //MARK:- 延时
    func doInBackground(_ completion:@escaping ()->()){
        let delay:TimeInterval = 0.35
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delay) {
            completion()
        }
    }
    
}

//MARK: - UICollectionViewDataSource
extension RecommendViewController: UICollectionViewDelegateFlowLayout{
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 1 {
            //1.取出 PrettyViewCell
            let prettyCell = collectionView.dequeueReusableCell (withReuseIdentifier: kPrettyCellID, for: indexPath) as!CollectionPrettyViewCell
            //2.设置数据
            prettyCell.anchor = recommendVM.anchorGroup[indexPath.section].anchors[indexPath.item]
            
            return prettyCell
        } else {
            return  super.collectionView(collectionView, cellForItemAt: indexPath)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 1 {
            return CGSize(width: kNormalItemW, height: kPrettyItemH)
        }
        return CGSize(width: kNormalItemW, height: kNormalItemH)
    }
}
